package com.stations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stations.common.web.domain.request.PageDomain;
import com.stations.common.web.domain.response.Result;
import com.stations.domain.*;
import com.stations.domain.dtree.CheckArr;
import com.stations.domain.dtree.DTree;
import com.stations.mapper.TStationMapper;
import com.stations.mapper.TTransmitterMapper;
import com.stations.service.TransmitterSendService;
import com.stations.service.TrasmiterService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Describe: 用户服务实现类
 * Author: Ljy
 * CreateTime: 2019/10/23
 */
@Service
public class TrasmiterServiceImpl extends ServiceImpl<TTransmitterMapper, TTransmitter> implements TrasmiterService {

    @Resource
    private TTransmitterMapper tTransmitterMapper;

    @Resource
    TStationMapper tStationMapper;

    @Resource
    TransmitterSendService transmitterSendService;

    @Resource(name = "TerminalRedisTemplate")
    RedisTemplate<String, Object> redisTemplate;
    /**
     * 根据发射机ID获取发射机任务列表
     * @param stationCode
     * @return
     */
    @Override
    public List<TTransmitter> selectTTransmitter(Integer stationCode){
        List<TTransmitter> lisTTransmitter = tTransmitterMapper.selectTTransmitterByStation(stationCode);
        if(lisTTransmitter.size() >0){
            ExecutorService executorService = Executors.newFixedThreadPool(lisTTransmitter.size());
            for (TTransmitter tTransmitter:lisTTransmitter) {
                executorService.submit(new Runnable() {
                @Override
                public synchronized void run(){
                    transmitterSendService.queryStatus(tTransmitter);
                    }
                });
            }
            executorService.shutdown();
        }
        return lisTTransmitter;
    }

    /**
     * 获取台站发射机树形结构
     * @return
     */
    @Override
    public List<DTree> getTree(){
        List<TStation> lis = tStationMapper.selectStations();
        return toTTransmitterTree(lis);
    }
    /**
     * Describe: 递归获取菜单tree
     * Param: sysMenus
     * Return: 操作结果
     */
    public List<DTree> toTTransmitterTree(List<TStation> lisTStation) {
        List<DTree> listDTree = new ArrayList<>();
        DTree dTreeP;
        CheckArr checkArr =new CheckArr();
        List<CheckArr> checkArrs=new ArrayList<>();
        checkArr.setType("0");
        checkArr.setIsChecked("0");
        checkArrs.add(checkArr);
        HashMap<String,String> hash = new HashMap<>();
        for (TStation tStation : lisTStation) {
            dTreeP = new DTree();
            dTreeP.setId(tStation.getCode());
            dTreeP.setParentId("0");
            dTreeP.setTitle(tStation.getName());
            dTreeP.setCheckArr(checkArrs);
            dTreeP.setIconClass("-1");
            listDTree.add(dTreeP);
        }
        List<DTree> treeDTrees =new  ArrayList<DTree>();
        for(DTree treeNode : getRootNode(listDTree)) {
            treeNode=buildChilTree(treeNode,listDTree);
            treeDTrees.add(treeNode);
        }
        return treeDTrees;
    }
    //递归，建立子树形结构
    public DTree buildChilTree(DTree pNode,List<DTree> listRegion){
        List<DTree> chilMenus =new  ArrayList<DTree>();
        for(DTree regionNode : listRegion) {
            if(regionNode.getParentId().equals(pNode.getId())) {
                chilMenus.add(buildChilTree(regionNode,listRegion));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点(获取所有的父节点)
    public List<DTree> getRootNode(List<DTree> listDTree) {
        List<DTree> rootRegionLists =new  ArrayList<DTree>();
        for(DTree regionNode : listDTree) {
            if(regionNode.getParentId().equals("0")) {
                rootRegionLists.add(regionNode);
            }
        }
        return rootRegionLists;
    }


    /**
     * 获取发射机列表
     * @param
     * @return
     */
    @Override
    public List<TTransmitter> getAllTransmitter(){
        List<TTransmitter> lisTTransmitter = tTransmitterMapper.getAllTransmitter();
        return lisTTransmitter;
    }

    /**
     * 删除发射机列表
     * @param
     * @return
     */
    @Override
    public Result delTransmitter(TTransmitter tTransmitter) {
        tTransmitterMapper.delTransmitter(tTransmitter.getStationCode(),tTransmitter.getTransCode());
        return Result.success("删除成功");
    }

    /**
     * 添加发射机
     * @param
     * @return
     */
    @Override
    public Result addTransmitter(TTransmitter tTransmitter) {
        tTransmitter.setCreateTime(new Date());
        tTransmitter.setSendTime(new Date());
        tTransmitter.setStatus(0);
        tTransmitterMapper.insert(tTransmitter);
        return Result.success("添加成功");
    }

    /**
     * 编辑发射机
     * @param
     * @return
     */
    @Override
    public Result editTransmitter(TTransmitter tTransmitter) {
        tTransmitter.setSendTime(new Date());
        QueryWrapper<TTransmitter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_code",tTransmitter.getStationCode());
        queryWrapper.eq("trans_code",tTransmitter.getTransCode());
        tTransmitterMapper.update(tTransmitter,queryWrapper);
        return Result.success("编辑成功");
    }

    @Override
    public PageInfo<TTransmitter> selectTTransmitterPage(TTransmitter tTransmitter, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<TTransmitter> data = tTransmitterMapper.selectTTransmitterList(tTransmitter);
        return new PageInfo<>(data);
    }

    @Override
    public RunningStatus    getTransmitterRunningStatus(RunningStatus runningStatus) {
        RunningStatus runningStatusnow = (RunningStatus) redisTemplate.boundHashOps("TransmitterRunningStatus").get(runningStatus.getStationCode() + "_" + runningStatus.getTransCode());
        return runningStatusnow;
    }
}
